package com.poc.party.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.meta.common.CommonInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.meta.cache.NdCacheService;
import com.meta.common.ConstDef;
import com.meta.nd.service.NvService;
import com.meta.nd.vo.FieldCondition;
import com.meta.nd.vo.ListQueryCondition;
import com.meta.nd.vo.ListQueryResult;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import com.poc.party.service.PartyService;
import com.poc.party.tool.PartyNvTool;
import com.poc.party.vo.PartyVO;
import com.poc.party.vo.QueryPartyCondition;


@Service
public class PartyServiceImpl implements PartyService {
	
	@Autowired
	NdCacheService ndCacheService;
	
	
	@Autowired
	NvService nvService;

	@Override
	public long saveParty(PartyVO partyVO) {
		
		Nd nd = ndCacheService.getNd(ConstDef.ND_CODE_PARTY_ND);
	
		Nv nv = PartyNvTool.partyVO2Nv(partyVO, nd);
		
		long id = nvService.saveNv(CommonInfo.getDefault(),nv);
			
		return id;
	}
	

	@Override
	public PartyVO getParty(long id) {
		
		Nd nd = ndCacheService.getNd(ConstDef.ND_CODE_PARTY_ND);
		
		Nv nv = this.nvService.getNv(CommonInfo.getDefault(),nd.getNdCode(), id);

		PartyVO party = PartyNvTool.nv2PartyVO(nv);
		
		return party;
	}
	
	@Override
	
	public PartyVO getPartyByCode( String partyCode ) {
			
		Nv nv = this.nvService.getNvByCode(CommonInfo.getDefault(),partyCode,"partyCode", ConstDef.ND_CODE_PARTY_ND);

		PartyVO party = PartyNvTool.nv2PartyVO(nv);
		
		return party;		
	}

	@Override
	public ListQueryResult<PartyVO> filterParties(QueryPartyCondition condition, int startNo, int count) {

		ListQueryCondition listQueryCondition = new ListQueryCondition();
		
		listQueryCondition.setCount(count);
		listQueryCondition.setStartNo(startNo);
		listQueryCondition.setNdCode(ConstDef.ND_CODE_PARTY_ND);
		
		if (condition.getPartyName() !=null && !condition.getPartyName().equals("")) {
			FieldCondition fieldCondition = new FieldCondition();
			fieldCondition.setFdName("partyName");
			fieldCondition.setLogicalOperation(ConstDef.LOGIC_OPERATION_EQUAL);
			fieldCondition.setValue(condition.getPartyName());
			listQueryCondition.getFieldCondition().add(fieldCondition);
		}
		
		
		if (condition.getCertId() !=null && !condition.getCertId().equals("")) {
			FieldCondition fieldCondition = new FieldCondition();
			fieldCondition.setFdName("certId");
			fieldCondition.setLogicalOperation(ConstDef.LOGIC_OPERATION_EQUAL);
			fieldCondition.setValue(condition.getCertId());
			listQueryCondition.getFieldCondition().add(fieldCondition);
		}
		
		
		if (condition.getMobileNo() !=null && !condition.getMobileNo().equals("")) {
			FieldCondition fieldCondition = new FieldCondition();
			fieldCondition.setFdName("mobileNo");
			fieldCondition.setLogicalOperation(ConstDef.LOGIC_OPERATION_EQUAL);
			fieldCondition.setValue(condition.getMobileNo());
			listQueryCondition.getFieldCondition().add(fieldCondition);
		}
		
		
		
		ListQueryResult<Nv> results = 	this.nvService.filterNvs(CommonInfo.getDefault(),listQueryCondition);
		
		ListQueryResult<PartyVO> partyResults = new ListQueryResult<PartyVO>();
		
		partyResults.setSuccess(results.isSuccess());
		partyResults.setTotalCount(results.getTotalCount());
		partyResults.setMessage(results.getMessage());

		List<PartyVO> partyVOs = new ArrayList<PartyVO>();
		for( Nv nv : results.getResult() ) {
			PartyVO party = PartyNvTool.nv2PartyVO(nv);
			partyVOs.add(party);
		}
		
		partyResults.setResult(partyVOs);
		return partyResults;
	}

}
